using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VWBPIH
    {
        public DataRow Convert2WBPIH(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryWBPIH(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["WBIMANDANT"] = " ".PadRight(2 ); 
         dwReturn["WBIRWKRS"] = " ".PadRight(2 ); 
         dwReturn["WBIRWLFD"] = " ".PadRight(6 ); 
         dwReturn["WBIPOS"] = " ".PadRight(4 ); 
         dwReturn["WBIUPOS"] = " ".PadRight(4 ); 
         dwReturn["WBINEUBEN"] = " ".PadRight(8 ); 
         dwReturn["WBINEUDAT"] = " ".PadRight(0 ); 
         dwReturn["WBIAENDBEN"] = " ".PadRight(8 ); 
         dwReturn["WBIAENDDAT"] = " ".PadRight(0 ); 
         dwReturn["WBIAENDZL"] = " ".PadRight(0 ); 
         dwReturn["WBIKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["WBISTKNR"] = " ".PadRight(12 ); 
         dwReturn["WBISHSTKNR"] = " ".PadRight(12 ); 
         dwReturn["WBISHSTK"] = " ".PadRight(0 ); 
         dwReturn["WBISHBRT"] = " ".PadRight(0 ); 
         dwReturn["WBISHSTKLG"] = " ".PadRight(0 ); 
         dwReturn["WBIVERLBRT"] = " ".PadRight(0 ); 
         dwReturn["WBIVERLLG"] = " ".PadRight(0 ); 
         dwReturn["WBIVSVGUET"] = " ".PadRight(0 ); 
         dwReturn["WBIFILLER"] = " ".PadRight(22 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapWBPIH(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWBPIH(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWBPIH(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWBPIH(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertWBPIH(dwLocal, ITXDBVERSION);
            }
            UpdateWBPIH(dwLocal);
        }
        public void InsertWBPIH(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO WBPIH(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryWBPIH(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WBPIH WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM WBPIH  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "WBPIH";
            Return = Result;
            return Return;
        }
        public void DeleteWBPIH(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM WBPIH   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateWBPIH(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE WBPIH SET "
                    +"WBIMANDANT= :WBIMANDANT,"
                    +"WBIRWKRS= :WBIRWKRS,"
                    +"WBIRWLFD= :WBIRWLFD,"
                    +"WBIPOS= :WBIPOS,"
                    +"WBIUPOS= :WBIUPOS,"
                    +"WBINEUBEN= :WBINEUBEN,"
                    +"WBINEUDAT= :WBINEUDAT,"
                    +"WBIAENDBEN= :WBIAENDBEN,"
                    +"WBIAENDDAT= :WBIAENDDAT,"
                    +"WBIAENDZL= :WBIAENDZL,"
                    +"WBIKUNDNR= :WBIKUNDNR,"
                    +"WBISTKNR= :WBISTKNR,"
                    +"WBISHSTKNR= :WBISHSTKNR,"
                    +"WBISHSTK= :WBISHSTK,"
                    +"WBISHBRT= :WBISHBRT,"
                    +"WBISHSTKLG= :WBISHSTKLG,"
                    +"WBIVERLBRT= :WBIVERLBRT,"
                    +"WBIVERLLG= :WBIVERLLG,"
                    +"WBIVSVGUET= :WBIVSVGUET,"
                    +"WBIFILLER= :WBIFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("WBIMANDANT", OracleType.NVarChar).Value = dwData["WBIMANDANT"].ToString(); 
          or1.Parameters.Add("WBIRWKRS", OracleType.NVarChar).Value = dwData["WBIRWKRS"].ToString(); 
          or1.Parameters.Add("WBIRWLFD", OracleType.NVarChar).Value = dwData["WBIRWLFD"].ToString(); 
          or1.Parameters.Add("WBIPOS", OracleType.NVarChar).Value = dwData["WBIPOS"].ToString(); 
          or1.Parameters.Add("WBIUPOS", OracleType.NVarChar).Value = dwData["WBIUPOS"].ToString(); 
          or1.Parameters.Add("WBINEUBEN", OracleType.NVarChar).Value = dwData["WBINEUBEN"].ToString(); 
          or1.Parameters.Add("WBINEUDAT", OracleType.NVarChar).Value = dwData["WBINEUDAT"].ToString(); 
          or1.Parameters.Add("WBIAENDBEN", OracleType.NVarChar).Value = dwData["WBIAENDBEN"].ToString(); 
          or1.Parameters.Add("WBIAENDDAT", OracleType.NVarChar).Value = dwData["WBIAENDDAT"].ToString(); 
          or1.Parameters.Add("WBIAENDZL", OracleType.NVarChar).Value = dwData["WBIAENDZL"].ToString(); 
          or1.Parameters.Add("WBIKUNDNR", OracleType.NVarChar).Value = dwData["WBIKUNDNR"].ToString(); 
          or1.Parameters.Add("WBISTKNR", OracleType.NVarChar).Value = dwData["WBISTKNR"].ToString(); 
          or1.Parameters.Add("WBISHSTKNR", OracleType.NVarChar).Value = dwData["WBISHSTKNR"].ToString(); 
          or1.Parameters.Add("WBISHSTK", OracleType.NVarChar).Value = dwData["WBISHSTK"].ToString(); 
          or1.Parameters.Add("WBISHBRT", OracleType.NVarChar).Value = dwData["WBISHBRT"].ToString(); 
          or1.Parameters.Add("WBISHSTKLG", OracleType.NVarChar).Value = dwData["WBISHSTKLG"].ToString(); 
          or1.Parameters.Add("WBIVERLBRT", OracleType.NVarChar).Value = dwData["WBIVERLBRT"].ToString(); 
          or1.Parameters.Add("WBIVERLLG", OracleType.NVarChar).Value = dwData["WBIVERLLG"].ToString(); 
          or1.Parameters.Add("WBIVSVGUET", OracleType.NVarChar).Value = dwData["WBIVSVGUET"].ToString(); 
          or1.Parameters.Add("WBIFILLER", OracleType.NVarChar).Value = dwData["WBIFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
